package com.finger.userservice.controller;

import com.finger.commonutil.helper.JwtHelper;
import com.finger.modelservice.model.user.Patient;
import com.finger.modelservice.result.Result;
import com.finger.userservice.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("site/user/patient")
public class PatientController {

    @Autowired
    private PatientService patientService;

    // 查找全部就诊人信息
    @GetMapping("auth/findAllPatient")
    public Result findAllPatient(HttpServletRequest req){

        String userId
                = JwtHelper.getUserId(req.getHeader("token"));
        List<Patient> patients = patientService.findAllPatient(userId);

        return Result.ok(patients);
    }

    // 根据id查找就诊人信息
    @GetMapping("auth/findPatient/{id}")
    public Result findPatient(@PathVariable String id){

        Patient patient = patientService.getById(id);
        patientService.packParam(patient);

        return Result.ok(patient);
    }

    // 修改就诊人信息
    @PutMapping("auth/updatePatient")
    public Result updatePatient(@RequestBody Patient patient){

        patientService.updateById(patient);
        return Result.ok();
    }

    // 添加就诊人信息
    @PostMapping("auth/addPatient")
    public Result addPatient(@RequestBody Patient patient,HttpServletRequest req){

        patient.setUserId(JwtHelper.getUserId(req.getHeader("token")));

        patientService.save(patient);

        return Result.ok();
    }

    // 根据id删除就诊人信息
    @DeleteMapping("auth/deletePatient/{id}")
    public Result deletePatient(@PathVariable String id){

        patientService.removeById(id);

        return Result.ok();
    }

}
